package cn.example.freid.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



import cn.example.freid.util.StringUtil;
import net.sf.json.JSONObject;

/**
 * Servlet implementation class adminLogin
 */
@WebServlet("/Login.do")
public class AdminLogin extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminLogin() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String username = request.getParameter("username");
		String password = request.getParameter("password");
		JSONObject returnMsg = new JSONObject();
		
		if ((!StringUtil.isNullOrEmpty(username,password)) &&
				username.equals("admin@admin.com") && password.equals("123456")) {
			
			request.getSession().setAttribute("role", "admin");
			returnMsg.put("err", "0");
			returnMsg.put("msg", "login succeed");
			
		} else {
			
			returnMsg.put("err", "1");
			returnMsg.put("msg", "invalidate username or password");
		}
		response.getOutputStream().write(returnMsg.toString().getBytes("utf-8"));
	}

}
